package demo01.service;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import demo01.repository.HelloRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class HelloService {
    @Autowired
    private HelloRepository helloRepository;

    public String sayHi(String name){
        Entry entry = null;
        String result = null;

        try {
            entry = SphU.entry("sayHiRepository");
            result = helloRepository.sayHiRepository(name);
        } catch (BlockException e) {
            log.info("限流啦",e);
            return "当前限流规则生效 无法访问资源";
        } finally {
            if(entry != null){
                entry.exit();
            }
        }

        return result;
    }

}
